<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Minimum volume ellipsoid covering union of ellipsoids</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/cvxbook/Ch08_geometric_probs/html/min_vol_union_ellip.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Minimum volume ellipsoid covering union of ellipsoids</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.4.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Original version by Lieven Vandenberghe</span>
<span class="comment">% Updated for CVX by Almir Mutapcic - Jan 2006</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% We find a smallest ellipsoid containing m ellipsoids</span>
<span class="comment">% { x'*A_i*x + 2*b_i'*x + c &lt; 0 }, for i = 1,...,m</span>
<span class="comment">%</span>
<span class="comment">% Problem data:</span>
<span class="comment">% As = {A1, A2, ..., Am}:  cell array of m pos. def. matrices</span>
<span class="comment">% bs = {b1, b2, ..., bm}:  cell array of m 2-vectors</span>
<span class="comment">% cs = {c1, c2, ..., cm}:  cell array of m scalars</span>

<span class="comment">% ellipse data</span>
As = {}; bs = {}; cs = {};
As{1} = [ 0.1355    0.1148;  0.1148    0.4398];
As{2} = [ 0.6064   -0.1022; -0.1022    0.7344];
As{3} = [ 0.7127   -0.0559; -0.0559    0.9253];
As{4} = [ 0.2706   -0.1379; -0.1379    0.2515];
As{5} = [ 0.4008   -0.1112; -0.1112    0.2107];
bs{1} = [ -0.2042  0.0264]';
bs{2} = [  0.8259 -2.1188]';
bs{3} = [ -0.0256  1.0591]';
bs{4} = [  0.1827 -0.3844]';
bs{5} = [  0.3823 -0.8253]';
cs{1} = 0.2351;
cs{2} = 5.8250;
cs{3} = 0.9968;
cs{4} = -0.2981;
cs{5} = 2.6735;

<span class="comment">% dimensions</span>
n = 2;
m = size(bs,2);    <span class="comment">% m ellipsoids given</span>

<span class="comment">% construct and solve the problem as posed in the book</span>
cvx_begin <span class="string">sdp</span>
    variable <span class="string">Asqr(n,n)</span> <span class="string">symmetric</span>
    variable <span class="string">btilde(n)</span>
    variable <span class="string">t(m)</span>
    maximize( det_rootn( Asqr ) )
    subject <span class="string">to</span>
        t &gt;= 0;
        <span class="keyword">for</span> i = 1:m
            [ -(Asqr - t(i)*As{i}), -(btilde - t(i)*bs{i}), zeros(n,n);
              -(btilde - t(i)*bs{i})', -(- 1 - t(i)*cs{i}), -btilde';
               zeros(n,n), -btilde, Asqr] &gt;= 0;
        <span class="keyword">end</span>
cvx_end

<span class="comment">% convert to ellipsoid parametrization E = { x | || Ax + b || &lt;= 1 }</span>
A = sqrtm(Asqr);
b = A\btilde;

<span class="comment">% plot ellipsoids using { x | || A_i x + b_i || &lt;= alpha } parametrization</span>
noangles = 200;
angles   = linspace( 0, 2 * pi, noangles );

clf
<span class="keyword">for</span> i=1:m
  Ai = sqrtm(As{i}); bi = Ai\bs{i};
  alpha = bs{i}'*inv(As{i})*bs{i} - cs{i};
  ellipse  = Ai \ [ sqrt(alpha)*cos(angles)-bi(1) ; sqrt(alpha)*sin(angles)-bi(2) ];
  plot( ellipse(1,:), ellipse(2,:), <span class="string">'b-'</span> );
  hold <span class="string">on</span>
<span class="keyword">end</span>
ellipse  = A \ [ cos(angles) - b(1) ; sin(angles) - b(2) ];

plot( ellipse(1,:), ellipse(2,:), <span class="string">'r--'</span> );
axis <span class="string">square</span>
axis <span class="string">off</span>
hold <span class="string">off</span>
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling Mosek 9.1.9: 94 variables, 15 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 15              
  Cones                  : 1               
  Scalar variables       : 9               
  Matrix variables       : 6               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 1                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 15              
  Cones                  : 1               
  Scalar variables       : 9               
  Matrix variables       : 6               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 13
Optimizer  - Cones                  : 1
Optimizer  - Scalar variables       : 8                 conic                  : 3               
Optimizer  - Semi-definite variables: 6                 scalarized             : 85              
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 60                after factor           : 60              
Factor     - dense dim.             : 0                 flops                  : 3.60e+03        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   1.6e+00  1.0e+00  6.0e+00  0.00e+00   5.000000000e+00   0.000000000e+00   1.0e+00  0.00  
1   2.7e-01  1.7e-01  3.3e-01  2.94e-01   1.333260668e+00   2.965940792e-01   1.7e-01  0.01  
2   7.7e-02  4.8e-02  3.7e-02  1.60e+00   3.245334863e-01   1.071294909e-01   4.8e-02  0.01  
3   1.8e-02  1.1e-02  4.1e-03  1.44e+00   1.142900036e-01   7.290687311e-02   1.1e-02  0.01  
4   3.2e-03  2.0e-03  3.8e-04  9.29e-01   8.576919578e-02   7.790604088e-02   2.0e-03  0.01  
5   6.0e-04  3.7e-04  3.0e-05  8.94e-01   8.031824828e-02   7.881846217e-02   3.7e-04  0.01  
6   1.6e-05  9.9e-06  1.3e-07  9.96e-01   7.872673678e-02   7.868618068e-02   9.9e-06  0.01  
7   6.5e-07  4.0e-07  1.1e-09  1.00e+00   7.868325294e-02   7.868160602e-02   4.0e-07  0.01  
8   7.6e-08  4.7e-08  4.3e-11  1.00e+00   7.868166869e-02   7.868147564e-02   4.7e-08  0.01  
9   3.8e-09  2.4e-09  4.9e-13  1.00e+00   7.868147490e-02   7.868146527e-02   2.4e-09  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 7.8681474900e-02    nrm: 1e+00    Viol.  con: 1e-08    var: 1e-08    barvar: 0e+00    cones: 0e+00  
  Dual.    obj: 7.8681465270e-02    nrm: 3e+00    Viol.  con: 0e+00    var: 4e-16    barvar: 2e-09    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 9         time: 0.02    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.0786815
 
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="min_vol_union_ellip__01.png" alt=""> 
</div>
</div>
</body>
</html>